package cn.wangzg.course0603;

import android.os.Bundle;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;

import java.io.IOException;

import cn.wangzg.course0603.adapter.CommonAdapter;
import cn.wangzg.course0603.adapter.ViewHolder;
import cn.wangzg.course0603.bean.Article;
import cn.wangzg.course0603.view.XListView;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity3 extends AppCompatActivity {
    private XListView listArticle;

    private CommonAdapter<Article.DataBean.DatasBean> adapter;
    private int curPage=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        initView();
        initData();
    }
    //使用OkHttp加载数据
    private void initData() {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("https://www.wanandroid.com/article/list/0/json")
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                //textView.setText("加载失败！");
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    Gson gson=new Gson();
                    Article article=gson.fromJson(result,Article.class);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.setmDatas(article.getData().getDatas());
                            curPage++;
                        }
                    });
                }
            }
        });

    }

    private void initView() {
        //handler = new MyHandler(this);
        listArticle = findViewById(R.id.listArticle);
        listArticle.setPullLoadEnable(true);
        listArticle.setPullRefreshEnable(true);
        listArticle.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                initData();
                curPage=1;
                listArticle.stopRefresh();
            }

            @Override
            public void onLoadMore() {

                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url("https://www.wanandroid.com/article/list/"+curPage+"/json")
                        .build();
                Call call = client.newCall(request);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        //textView.setText("加载失败！");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        if (response.isSuccessful()) {
                            String result = response.body().string();
                            Gson gson=new Gson();
                            Article article=gson.fromJson(result,Article.class);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    adapter.addmDatas(article.getData().getDatas());
                                    curPage++;
                                    listArticle.stopLoadMore();
                                }
                            });
                        }
                    }
                });
            }
        });
        //使用通用适配器，建议大家掌握，但是考试时不考此内容
        adapter = new CommonAdapter<Article.DataBean.DatasBean>(this, null,
                R.layout.item_article) {
            @Override
            public void convert(ViewHolder helper, final Article.DataBean.DatasBean item) {
                helper.setText(R.id.textView, item.getTitle());
                helper.setText(R.id.textView3, item.getShareUser());
                helper.setText(R.id.textView5, item.getSuperChapterName()
                        + "/" + item.getChapterName());
                helper.setText(R.id.textView7, item.getNiceDate());
                helper.getView(R.id.imageView).setSelected(item.isCollect());
                helper.getView(R.id.imageView).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        v.setSelected(!v.isSelected());
                        item.setCollect(v.isSelected());
                    }
                });
            }
        };
        listArticle.setAdapter(adapter);
        //添加单击事件


    }
}
